/**
 * Picture.java
 *
 *
 * $LastChangedBy: allen $
 * $LastChangedDate: 2007-12-11 17:22:48 +0800 $
 * $Revision: 1440 $
 */
package com.myspace.domain;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

@Entity(name = "Picture")
public class Picture extends DomainObject {

	private static final long serialVersionUID = 20071116L;

	private Long id;

	private String name;

	private String previewPicPath;

	private String originalPicPath;

	private User user;

	private String note;

	private Date createDate;

	private List<Tag> tags = new LinkedList<Tag>();

	/**
	 * NOTE: If sets stragegy to GenerationType.SEQUENCE, it don't work well.
	 * HIbernate will user hilo as generation stragegy.
	 */
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_picture")
	@SequenceGenerator(name = "seq_picture", sequenceName = "SEQ_PICTURE")
	public Long getId() {
		return id;
	}

	void setId(Long id) {
		this.id = id;
	}

	@Basic(fetch = FetchType.EAGER)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "PREVIEW_PIC_PATH")
	public String getPreviewPicPath() {
		return previewPicPath;
	}

	public void setPreviewPicPath(String previewPicPath) {
		this.previewPicPath = previewPicPath;
	}

	@Column(name = "ORIGINAL_PIC_PATH")
	public String getOriginalPicPath() {
		return originalPicPath;
	}

	public void setOriginalPicPath(String originalPicPath) {
		this.originalPicPath = originalPicPath;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	@Column(name = "CREATE_DATE")
	@Temporal(TemporalType.TIMESTAMP)
	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	@ManyToMany(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
	@JoinTable(name = "REL_TAG_PICTURE", joinColumns = @JoinColumn(name = "PIC_ID", referencedColumnName = "ID"), inverseJoinColumns = @JoinColumn(name = "TAG_ID", referencedColumnName = "ID"))
	@Fetch(FetchMode.SUBSELECT)
	public List<Tag> getTags() {
		return tags;
	}

	public void setTags(List<Tag> tags) {
		this.tags = tags;
	}

	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name = "USER_ID", referencedColumnName = "ID", nullable = false, updatable = false)
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	@Override
	@Transient
	public boolean isPersistence() {
		return id != null;
	}

}
